﻿using Dapper;
using db_manage.model;
using db_manage.model.db;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace db_manage.dll.db
{
    public class db_mysql : db_base
    {
        public override void attach(db_info db_info, string path)
        {
            throw new NotImplementedException();
        }

        public override string bak(db_info db_info, db_bak_param db_bak_param)
        {
            var bak_name = $"{db_info.name}_{(db_bak_param.bak_diff ? "diff" : "full")}_{DateTime.Now:yyyyMMddHHmmss}.bak";
            var conn = (MySqlConnection)db_connection_fast_get(db_info);
            using (MySqlCommand cmd = new MySqlCommand())
            {
                using (MySqlBackup mb = new MySqlBackup(cmd))
                {
                    cmd.Connection = conn;
                    conn.Open();
                    mb.ExportToFile(@$"{db_bak_param.dir_bak}\\{bak_name}");
                    conn.Close();
                }
            }
            return bak_name;
        }

        public override DbConnection db_connection_get(db_info db_info)
        {
            if (string.IsNullOrEmpty(db_info.name))
            {
                db_info.name = "mysql";
            }
            var connStr = $"Server={db_info.db_link_info.ip}; Database={db_info.name}; User ID={db_info.db_link_info.username}; Password={db_info.db_link_info.pwd}";
            return new MySqlConnection(connStr);
        }

        public override List<db_info> db_info_list(db_link_info db_link_info)
        {
            var list = sql_query<db_info>(new db_info() { db_link_info = db_link_info },
                @"SELECT SCHEMA_NAME name 
FROM information_schema.SCHEMATA 
where SCHEMA_NAME not in ('sys','information_schema','performance_schema','mysql','test')
order by  SCHEMA_NAME asc").AsList();
            list?.ForEach(x => x.db_link_info = db_link_info);
            return list;
        }

        public override void db_record_create(db_info db_info)
        {
            //创建执行记录的表
            var db_record_create_sql = @"
CREATE TABLE If Not Exists `db_manage_record`  (
  `vernum` int NULL DEFAULT NULL COMMENT '版本号',
  `updatetime` datetime NULL DEFAULT NULL COMMENT '更新时间',
  `updatestatus` int NULL DEFAULT NULL COMMENT '更新状态，0失败，1成功',
  `remark` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '备注',
  `taketime` int NULL DEFAULT NULL,
  `sqlstr` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '脚本'
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
";
            sql_exec(db_info, db_record_create_sql);
        }

        public override db_struct db_struct_get(db_info db_info, bool is_sql = false)
        {
            throw new NotImplementedException();
        }

        public override void detach_move(db_info db_info, string path)
        {
            throw new NotImplementedException();
        }

        public override List<db_diff_info> diff_check(db_struct template_db_struct, db_info db_info)
        {
            throw new NotImplementedException();
        }

        public override void diff_fix(db_info db_info, List<db_diff_info> list)
        {
            throw new NotImplementedException();
        }

        public override void link_test(db_link_info db_link_info)
        {
            var db_test = new model.db_info()
            {
                db_link_info = db_link_info,
                name = "mysql"
            };
            sql_exec(db_test, "show databases");
        }
    }
}
